(function($) {
    $.cookie = function(key, value, options) {

        // key and at least value given, set cookie...
        if (arguments.length > 1 && (!/Object/.test(Object.prototype.toString.call(value)) || value === null || value === undefined)) {
            options = $.extend({}, options);

            if (value === null || value === undefined) {
                options.expires = -1;
            }

            if (typeof options.expires === 'number') {
                var days = options.expires, t = options.expires = new Date();
                t.setDate(t.getDate() + days);
            }

            value = String(value);

            return (document.cookie = [
                encodeURIComponent(key), '=', options.raw ? value : encodeURIComponent(value),
                options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
                options.path    ? '; path=' + options.path : '',
                options.domain  ? '; domain=' + options.domain : '',
                options.secure  ? '; secure' : ''
            ].join(''));
        }

        // key and possibly options given, get cookie...
        options = value || {};
        var decode = options.raw ? function(s) {return s;} : decodeURIComponent;

        var pairs = document.cookie.split('; ');
        for (var i = 0, pair; pair = pairs[i] && pairs[i].split('='); i++) {
            if (decode(pair[0]) === key) return decode(pair[1] || ''); // IE saves cookies with empty string as "c; ", e.g. without "=" as opposed to EOMB, thus pair[1] may be undefined
        }
        return null;
    };
})(jQuery);

jQuery(document).ready(function($) {
	
	var ccp;
	if($.cookie("main-color")) {
		sg_color($.cookie("main-color"));
	}
	if($.cookie("main-pattern")) {
		sg_pattern($.cookie("main-pattern"));
	}
	if($.cookie("main-pattern-img") && $.cookie("main-pattern-img") == "light") {
		$('#top-container').css({background: 'url(' + sg_template_url + '/demo/light.jpg) #e8e8e8 no-repeat center top'});
	}
	if($.cookie("main-color-p")) {
		ccp = $.cookie("main-color-p");
	} else {
		ccp = "#000000";
	}
	$('#wide-color div').css('backgroundColor', ccp);
	
	$('#stylesw').styleSwitcher();
	
	$('#wide-color').ColorPicker({
		color: ccp,
		onShow: function (colpkr) {
			$(colpkr).fadeIn(500);
			return false;
		},
		onHide: function (colpkr) {
			$(colpkr).fadeOut(500);
			return false;
		},
		onSubmit: function(hsb, hex, rgb, el) {
			$.cookie("main-color-p", '#' + hex, {expires: 365, path: '/'});
			$('#wide-color div').css('backgroundColor', '#' + hex);
			sg_color('#' + hex);
			$(el).ColorPickerHide();
		}
	});
	
	$('.open-close').click(function() {
		if ($(this).parent().css('left') == '-148px') {
			$(this).parent().animate({
				"left": "0"
			}, 300);
		} else {
			$(this).parent().animate({
				"left": "-148px"
			}, 300);
		}
	});
	
	function sg_color(color)
	{
		$.cookie("main-color", color, {expires: 365, path: '/'});
		sg_template_color = color;
		var hcolor = 'a:hover,p a,.clr,.short-tour h2 span,ul.navmenu li ul li a:hover,.tweet_text a,.post p span strong a:hover,#map-side-bar div.map-location a:hover,#footer-wrap div.tagcloud a:hover,p.auth-cat strong a:hover,.sidebar .widget_recent_comments a:hover {color: ' + color +';}' + "\n";
		var hborder = '.tipswift,ul.accordion li a.title:hover {border-color: ' + color +';}' + "\n";
		var hbg = '.nivo-caption p span,.nivo-caption p p,.breadcramp p,.breadcramp span,.tipswift-inner,.proj-img a:hover,.proj-img a.zoom:hover,.search-btn input:hover,#search div.search-btn input:hover,ul.accordion li a.title:hover,span.highlight-hl-theme,.inner-pages-slider div.cont a.hire {background-color: ' + color +';}';
		$('#main-color-css').html(hcolor + hborder + hbg);
	}
	
	function sg_pattern(pattern)
	{
		$.cookie("main-pattern", pattern, {expires: 365, path: '/'});
		$('#top-container').css({background: 'url(' + sg_template_url + '/images/patterns/' + pattern + '.png) #e8e8e8 repeat'});
		$('#footer-wrap').css({background: 'url(' + sg_template_url + '/images/patterns/' + pattern + '-footer.png) #282828 repeat'});
	}
	
	$('.theme-changer-wrap a.color-themes').click(function() {
		if ($(this).attr('rel') != "#") sg_color($(this).attr('rel'));
		return false;
	});
	
	$('.theme-changer-wrap a.pat').click(function() {
		$.cookie("main-pattern-img", "", {expires: 365, path: '/'});
		sg_pattern($(this).attr('rel'));
		return false;
	});
	
	$('#wide-image').click(function() {
		$.cookie("main-pattern-img", "light", {expires: 365, path: '/'});
		$('#top-container').css({background: 'url(' + sg_template_url + '/demo/light.jpg) #e8e8e8 no-repeat center top'});
		return false;
	});

});
